/*
 * @lc app=leetcode.cn id=155 lang=typescript
 *
 * [155] 最小栈
 */

// @lc code=start

//  思路：使用辅助栈
//  一个数组用作栈存储数据，一个辅助栈存储到当前下标对应的最小值

//  复杂度：O(1) O(n)

class MinStack {
    protected stack: number[] = []
    protected minNum: number[] = []

    constructor() {

    }

    push(val: number): void {
        this.stack.push(val)
        // ?? 如果变量值为 null 或者 undefined，刚使用双问号后的默认值，否则使用该变量值。
        this.minNum.push(Math.min(val, this.minNum[this.minNum.length - 1] ?? Infinity))
    }

    pop(): void {
        this.stack.pop()
        this.minNum.pop()

    }

    top(): number {
        return this.stack[this.stack.length - 1]
    }

    getMin(): number {
        return this.minNum[this.minNum.length - 1]
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * var obj = new MinStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.getMin()
 */
// @lc code=end

const minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin();
minStack.pop();
minStack.top();
minStack.getMin();